package cn.com.api.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiU_stat_keywords;

import cn.com.api.mapper.ApiU_stat_keywordsMapper;

import cn.com.api.service.ApiU_stat_keywordsService;
import cn.com.api.service.impl.base.U_stat_keywordsServiceImplBase;
import cn.com.util.Util;

/**
 * 
 **/
@Service
public class ApiU_stat_keywordsServiceImpl extends U_stat_keywordsServiceImplBase implements ApiU_stat_keywordsService {
	@Autowired
	private ApiU_stat_keywordsMapper apiU_stat_keywordsMapper;

	// 新增对象后返回记录ID
	public int U_stat_keywordsInsertion(ApiU_stat_keywords u_stat_keywords) {
		return this.apiU_stat_keywordsMapper.U_stat_keywordsInsertion(u_stat_keywords);
	}

	// 记录喊红包次数
	public int SaveBonusVisitCount(Map map) {
		int ret = 0;
		List<ApiU_stat_keywords> keywords_list = this.apiU_stat_keywordsMapper.findU_stat_keywordssByMap(map);
		if (!Util.isNull(keywords_list) && keywords_list.size() > 0) {
			// update
			ApiU_stat_keywords keyword = keywords_list.get(0);
			Integer visit_count = keyword.getVisit_count();
			keyword.setVisit_count(visit_count + 1);
			keyword.setUpdate_time(Util.CurrentTime());
			ret = this.apiU_stat_keywordsMapper.updateU_stat_keywords(keyword);
		} else {
			// Insert
			ApiU_stat_keywords keyword = new ApiU_stat_keywords();
			keyword.setCreate_time(Util.CurrentTime());
			keyword.setUpdate_time(Util.CurrentTime());
			keyword.setKeyword(map.get("keyword").toString());
			keyword.setSource(Integer.valueOf(map.get("source").toString()));
			keyword.setType(map.get("type").toString());
			keyword.setVisit_count(1);
			ret = this.apiU_stat_keywordsMapper.insertU_stat_keywords(keyword);
		}
		return ret;
	}

	public List<ApiU_stat_keywords> keywordsCount() throws Exception {
		return this.apiU_stat_keywordsMapper.keywordsCount();
	}
}
